package com.approval.approvalapp.index.cygn.wzly;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.WzlyListBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by louyulin on 2019/5/7.
 */

public class WzlyQdAdapter extends RecyclerView.Adapter<WzlyQdAdapter.Holder> {

    List<WzlyListBean> list1;
    Context context;
    ClickRvListener listener;


    public void setList1(List<WzlyListBean> list1 , ClickRvListener listener) {
        this.list1 = list1;
        this.listener = listener;
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.wzlyqd_item, parent, false);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        holder.tv1.setText(list1.get(position).getName());
        holder.tv2.setText(list1.get(position).getRedcount() + "件");
        holder.rv.setLayoutManager(new LinearLayoutManager(context));
        ItemAdapter itemAdapter = new ItemAdapter();
        itemAdapter.setListBeanList(list1.get(position).getList() , position);
        holder.rv.setAdapter(itemAdapter);
    }

    @Override
    public int getItemCount() {
        if (list1 == null) {
            return 0;
        } else {
            return list1.size();
        }
    }

    class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv1)
        TextView tv1;
        @Bind(R.id.tv2)
        TextView tv2;
        @Bind(R.id.rv)
        RecyclerView rv;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemHolder> {
        List<WzlyListBean.ListBean> listBeanList;
        int itemindex;


        public void setListBeanList(List<WzlyListBean.ListBean> listBeanList , int itemindex) {
            this.listBeanList = listBeanList;
            this.itemindex = itemindex;
            notifyDataSetChanged();
        }

        @Override
        public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_item_xzlyqd, parent, false);
            ItemHolder itemHolder = new ItemHolder(view);
            return itemHolder;
        }

        @Override
        public void onBindViewHolder(ItemHolder holder, int position) {
                holder.tv1.setText(listBeanList.get(position).getName());
                holder.tv2.setText(listBeanList.get(position).getCurrent_count() + "");

                holder.editIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.listener(list1.get(itemindex).getId());
                    }
                });
        }

        @Override
        public int getItemCount() {
            if (listBeanList == null) {
                return 0;
            } else {
                return listBeanList.size();
            }
        }

        class ItemHolder extends RecyclerView.ViewHolder {
            @Bind(R.id.tv1)
            TextView tv1;
            @Bind(R.id.edit_iv)
            ImageView editIv;
            @Bind(R.id.tv2)
            TextView tv2;
            public ItemHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }

    public interface ClickRvListener {
        void listener(String id);
    }

}
